package com.yct.springcloud.controller;

import com.yct.springcloud.loadBance.MyLoadBancer;
import com.yct.springcloud.serveice.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.UUID;

@RestController
@Slf4j
public class OrderController {

    @Autowired
    OrderService orderService;
    @Autowired
    RestTemplate restTemplate;

    @Autowired
    DiscoveryClient discoveryClient;

    @Autowired
    MyLoadBancer myRotationLoadBancer;

    @Value("${service-url.nacos-user-service}")
    private String serverURL;

    @GetMapping(value = "/consumer/payment/nacos/{id}")
    public String paymentInfo(@PathVariable("id") Long id)
    {
        log.info("**kkkkkkkkkkkkkkkkk***根据id【" + id + "】查询支付数据。");
        discoveryClient.getServices().stream().forEach(System.out::println);
        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("nacos-payment-service");
        return restTemplate.getForObject(serverURL+"/payment/nacos/"+id,String.class);
    }

    @GetMapping(value = "/consumer/service/selfLoadBancer/nacos/{serviceId}")
    public ServiceInstance getService(@PathVariable("serviceId") String serviceId)
    {
        List<ServiceInstance> serviceInstances = discoveryClient.getInstances(serviceId);

        return myRotationLoadBancer.getSingleLoadBancer(serviceInstances);
    }

}
